package acl

import (
	"fmt"

	us "gitee.com/gouyabin/go_utils"
)

type User struct {
	Username string
	Passwd   string
}

// type Users struct {
// 	List []User
// }

var UsersList []User

type aclAuth struct {
	config *ACLConfig
}

func init() {
	fmt.Println("初始化用户")
	UsersList = []User{}
	myConfig := new(us.Config)
	myConfig.InitConfig("./user.config")
	user := myConfig.Read("User", "user")
	passwd := myConfig.Read("User", "passwd")
	UsersList = append(UsersList, User{
		Username: user,
		Passwd:   passwd,
	})

}

func Init() *aclAuth {
	// aclConfig, err := AclConfigLoad("./plugins/auth/authfile/acl.conf")
	aclConfig, err := AclConfigLoad("./acl.conf")
	if err != nil {
		panic(err)
	}
	return &aclAuth{
		config: aclConfig,
	}
}

func (a *aclAuth) CheckConnect(clientID, username, password string) bool {
	for _, p := range UsersList {
		if username == p.Username && password == p.Passwd {
			fmt.Println("认证通过")
			return true
		}
	}
	fmt.Println("认证失败")
	return false

}

func (a *aclAuth) CheckACL(action, clientID, username, ip, topic string) bool {
	return checkTopicAuth(a.config, action, ip, username, clientID, topic)
}
